package com.hospital.BaseActivity;

import android.app.TabActivity;
import android.content.*;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by wangjun on 2015/7/2.
 */
public class IndexBaseActivity extends TabActivity {

    private final String ACTION_NAME = "newMessage";
    private SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sp = getSharedPreferences("user_msg", 0);
        registerBoradcastReceiver();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        RemoteService.ReceiveMessageToLocation(this, new Handler(){
//            public void  handleMessage(Message msg) {
//                int userId = sp.getInt("userId", 0);
//                if (userId == 0) return;
//                int unreadCount = CommonDao.getInstance(IndexBaseActivity.this).getUnreadMessageCount(userId);
//                ImageView iv_unread = (ImageView) findViewById(R.id.msg_item_unread);
//                if(iv_unread == null) return;
//                iv_unread.setVisibility(unreadCount > 0 ? View.VISIBLE : View.GONE);
//            }
//        });
    }

    private Handler newMessageHandler = new Handler(){
        public void  handleMessage(Message msg) {
           // ImageView iv_unread = (ImageView) findViewById(R.id.msg_item_unread);
           // if(iv_unread == null) return;
           // iv_unread.setVisibility(View.VISIBLE);
        }
    };


    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals(ACTION_NAME)){
                newMessageHandler.sendMessage(new Message());
            }
        }

    };

    public void registerBoradcastReceiver(){
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTION_NAME);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

}
